package com.ninth_group.service.Impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ninth_group.constant.TFDConstants;
import com.ninth_group.dao.PawDao;
import com.ninth_group.dao.TFHDataDao;
import com.ninth_group.exception.province.ProvinceNameIsValidException;
import com.ninth_group.mapper.TFHDataMapper;
import com.ninth_group.dto.CityDTO;
import com.ninth_group.exception.city.CityException;
import com.ninth_group.po.TFHData;
import com.ninth_group.service.TFHDataService;
import com.ninth_group.utils.HiveUtil;
import com.ninth_group.utils.ReuseCodeService;
import com.ninth_group.vo.TFHDataVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 24小时天气数据服务实现类
 */
@Service
public class TFHDataServiceImpl implements TFHDataService {
    @Resource
    private TFHDataDao TFHDataDao;
    @Resource
    private HiveUtil hiveUtil;
    @Resource
    private ReuseCodeService reuseCodeService;
    @Resource
    private PawDao pawDao;

    /**
     * 根据城市和省份名称获取城市信息
     * @param cityDTO 城市信息
     * @return 二十四小时天气信息
     */
    @Override
    public List<TFHDataVO> getCityTwentyHcData(CityDTO cityDTO)
            throws JsonProcessingException {

        if (cityDTO == null) throw new CityException("城市信息为空");

        // 查询城市ids
        List<String> cityIds = reuseCodeService.getCityInfoByCid(
                cityDTO.getProvinceName(), cityDTO.getCityName(), hiveUtil);

        if (cityIds == null || cityIds.isEmpty()) throw new CityException("城市不存在");

        // 查询redis缓存
        List<TFHDataVO> voList = reuseCodeService
                .getDataFromRedis(cityIds, TFHDataVO.class,
                        TFDConstants.F24H_DATA, TFDConstants.C);

        // redis存在缓存数据
        if (!voList.isEmpty()) return voList;


        // 查询数据库并添加到redis中
        List<TFHData> city24Info = TFHDataDao.getCityTwentyForHoursData(cityIds);

        List<TFHDataVO> vo = TFHDataMapper.INSTANCE.toVo(city24Info);

        reuseCodeService.addToRedis(vo,
                TFDConstants.CITY_WEATHER_24H_C_INFO,
                TFHDataVO::getCid,
                TFDConstants.CACHE_TIME, TimeUnit.MINUTES);

        return vo;
    }

    /**
     * 获取省会24小时天气数据
     * @param pName 省份名称
     * @return 24小时天气数据
     */
    @Override
    public List<TFHDataVO> getCityTwentyHpData(String pName) throws JsonProcessingException {
        if (pName == null || pName.isEmpty())
            throw new ProvinceNameIsValidException("省份名称不能为空");

        // 查询省会表获取城市id
        String cid = pawDao.getPid(pName);

        // 查询redis缓存
        // 第三个参数: true -> 24小时数据 false -> 7天数据
        // 第四个参数: true -> 省份 false -> 城市
        List<TFHDataVO> voList =
                reuseCodeService.getDataFromRedis(List.of(cid),
                        TFHDataVO.class, TFDConstants.F24H_DATA, TFDConstants.P);

        // redis存在缓存数据
        if (!voList.isEmpty()) return voList;

        // 查询数据库并添加到redis中
        List<TFHData> city24Info = TFHDataDao.getCityTwentyForHoursData(List.of(cid));

        List<TFHDataVO> vo = TFHDataMapper.INSTANCE.toVo(city24Info);

        // 添加数据到redis
        reuseCodeService.addToRedis(vo,
                TFDConstants.CITY_WEATHER_24H_P_INFO,
                TFHDataVO::getCid,
                TFDConstants.CACHE_TIME, TimeUnit.MINUTES);

        return vo;
    }

}
